package com.example.newstudent.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.example.newstudent.entity.Result;
import com.example.newstudent.service.bulletinService;
import com.example.newstudent.service.lessonService;
import com.example.newstudent.service.studentService;
import com.example.newstudent.service.userService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@CrossOrigin
@RestController
@RequestMapping("/screen")
public class screen {
    @Autowired
    userService userService;
    @Autowired
    studentService studentService;
    @Autowired
    lessonService lessonService;
    @Autowired
    bulletinService bulletinService;
    @PostMapping("/getStudentHaveStatus")
    public Result getStudentHaveStatus(){
        System.out.println("大屏需要已经报道的学生数量");
        return Result.success(studentService.getStudentHaveStatus());
    }
    @PostMapping("/gender")
    public Result gender(){
        System.out.println("大屏需要男女人数");
        return Result.success(studentService.gender());
    }
    @PostMapping("/province")
    public Result province(){
        System.out.println("大屏需要省份人数");
        return Result.success(studentService.provinces());
    }
    @PostMapping("/status")
    public Result haveStatus(){
        System.out.println("大屏需要报道时间和人数");
        List<Integer> integers =new ArrayList<>();
        List<String> list = new ArrayList<>();
        List<LocalDateTime> localDateTimeList = studentService.haveStatus();
        System.out.println("时间"+localDateTimeList);
        Map<LocalDate, List<LocalDateTime>> groupedByDate = localDateTimeList.stream()
                .collect(Collectors.groupingBy(
                        LocalDateTime::toLocalDate, // 提取LocalDate
                        Collectors.toList() // 收集到列表中
                ));
// 现在groupedByDate包含了按年月日分组的LocalDateTime列表
        for (Map.Entry<LocalDate, List<LocalDateTime>> entry : groupedByDate.entrySet()) {
            // 如果需要，可以进一步处理每个分组的列表
            list.add(String.valueOf(entry.getKey()));
            integers.add(entry.getValue().size());
        }
        Map<Object,Object> map = new HashMap<>();
        map.put("time",list);
        map.put("num",integers);
        System.out.println(map);
        return Result.success(map);
    }
    @PostMapping("/academy")
    public Result academy(){
        System.out.println("大屏需要学院人数");
        List<String> strings = studentService.academy();
        System.out.println(strings);
        List<String> list = new ArrayList<>();
        List<Integer> list1 = new ArrayList<>();
        Map<String,Integer>  stringCountMap = new HashMap<>();
        Map<Object,Object>  map = new HashMap<>();
        for (String str : strings) {
            stringCountMap.put(str, stringCountMap.getOrDefault(str, 0) + 1);
        }

        // 现在，我们要找出出现次数相同的数量
        Map<Integer, Integer> countOfCounts = new HashMap<>();

        // 遍历Map中的计数并更新countOfCounts
        for (int count : stringCountMap.values()) {
            countOfCounts.put(count, countOfCounts.getOrDefault(count, 0) + 1);
        }
        for (Map.Entry<String, Integer> entry : stringCountMap.entrySet()) {
            list.add(entry.getKey());
            list1.add(entry.getValue());
        }
        // 打印结果
        map.put("academy",list);
        map.put("num", list1);
        return Result.success(map);
    }
}
